// pages/users/bindwechat/index.js
const customRequst = require('../../../common/customRequst');
const customUser = require('../../../common/commonUser');
const app = getApp();
Page({
  data: {
    nickName: '',
    avatarUrl: '',
    saveUrl: '',
    formToken: '',
  },

  onLoad: function (options) {
    let that = this;
    let userInfo = customUser.getUserInfo();
    if (userInfo) {
      that.setData({
        nickName: userInfo.nickname,
        avatarUrl: userInfo.avatar,
        saveUrl: userInfo.avatar,
      })
    };

    customRequst.postRequest('api/common/get_form_token').then(res => {
      if (res.code == 1) {
        that.setData({
          formToken: res.data.token
        })
      }
    });

  },

  onShow() {
    if (app.globalData.imgSrc) {
      this.setData({
        avatarUrl: app.globalData.imgSrc
      })
      console.log(app.globalData.imgSrc)
    }
  },
  
  //上传头像
  upIMG() {
    let that = this;
    wx.navigateTo({
      url: `/pages/users/corpper/corpper?imgSrc=${that.data.avatarUrl}`,
    })
    // wx.chooseImage({
    //   count: 1,
    //   sizeType: ["original", "compressed"],
    //   sourceType: ["album", "camera"],
    //   success: function (res) {
    //     const tempFilePaths = res.tempFilePaths
    //     wx.uploadFile({
    //       url: app.globalData.workAipDomain + 'api/common/upload_file/',
    //       filePath: tempFilePaths[0],
    //       name: 'file',
    //       formData: {
    //         'user': 'test'
    //       },
    //       success(res) {
    //         let resData = JSON.parse(res.data)
    //         that.setData({
    //           avatarUrl: resData.data.fullurl,
    //           saveUrl: resData.data.url,
    //         })
    //       }
    //     })
    //   }
    // })
  },

  inputValue(e) {
    let that = this
    that.setData({
      nickName: e.detail.value
    })
  },

  save() {
    let that = this
    customRequst.postRequest('api/user/edit', {
      nickname: that.data.nickName,
      avatar: that.data.avatarUrl,
      token: that.data.formToken
    }).then(res => {
      if (res.code == 1) {
        wx.showToast({
          title: '修改成功',
          icon: 'success',
          duration: 2000
        });

        //修改缓存数据
        let updateUserInfo = [{
            'key': 'nickname',
            'value': res.data.nickname
          },
          {
            'key': 'avatar',
            'value': res.data.avatar
          }
        ]
        customUser.updateUserInfo(updateUserInfo);
        setTimeout(function () {
          wx.switchTab({
            url: '/pages/users/index/index',
          })
        }, 2000)
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        });
        that.setData({
          formToken: res.data.token
        })
      }
    })
  },

  cropperload(e) {
    console.log("cropper初始化完成");
  },
  loadimage(e) {
    console.log("图片加载完成", e.detail);
    wx.hideLoading();
    //重置图片角度、缩放、位置
    this.cropper.imgReset();
  },
  clickcut(e) {
    console.log(e.detail);
    //点击裁剪框阅览图片
    wx.previewImage({
      current: e.detail.url, // 当前显示图片的http链接
      urls: [e.detail.url] // 需要预览的图片http链接列表
    })
  },
})